﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace Luka;

/// <summary>
/// 规范结果扩展
/// </summary>
[NotScanned]
internal static class UnifyResultExtensions
{
    /// <summary>
    /// 检查是否是有效
    /// </summary>
    public static bool CheckVaild(IActionResult? result, out object? data)
    {
        data = default;
        var isDataResult = result switch
        {
            FileResult => false,
            ChallengeResult => false,
            SignInResult => false,
            SignOutResult => false,
            RedirectToPageResult => false,
            RedirectToRouteResult => false,
            RedirectResult => false,
            RedirectToActionResult => false,
            LocalRedirectResult => false,
            ForbidResult => false,
            NotFoundResult => false,
            NotFoundObjectResult => false,
            _ => true,
        };
        if (isDataResult) data = result switch
        {
            ContentResult content => content.Content,
            ObjectResult obj => obj.Value,
            JsonResult json => json.Value,
            _ => null,
        };
        return isDataResult;
    }

    /// <summary>
    /// 返回值
    /// </summary>
    public static JsonResult ReturnResult(this FilterContext context, object? data, bool isFailed = false)
    {
        object? result;
        if (!context.HasAttribute<NoUnifyResultAttribute>() && context.HasFilter<UnifyResultFilter>())
        {
            if (data is BaseResult baseReult)
            {
                result = baseReult;
            }
            else if (!isFailed)
            {
                result = BaseResult.SetSuccessed(data);
            }
            else
            {
                if (data is ErrorMessageAttribute message)
                {
                    result = BaseResult.SetFailed(message?.Message, message?.Code ?? StatusCodes.Status400BadRequest);
                }
                else if (data is Exception badResult)
                {
                    result = BaseResult.SetFailed(badResult?.Message);
                }
                else if (data is string str)
                {
                    result = BaseResult.SetFailed(str);
                }
                else if (data is ValueType vt)//判断是否为值类型，能够满足大部分判断
                {
                    result = BaseResult.SetFailed(vt.ToString());
                }
                else
                {
                    result = BaseResult.SetFailed();
                }
            }
        }
        else
        {
            if (data is ErrorMessageAttribute message)
            {
                result = message?.Message;
            }
            else if (data is BaseResult baseReult)
            {
                result = baseReult.Data;
            }
            else if (data is Exception badResult)
            {
                result = badResult.Message;
            }
            else
            {
                result = data;
            }
        }
        return new JsonResult(result);
    }
}